#include "Bag.h"
#include "Map.h"
#include "Item.h"

Bag::Bag()
{
	initWithFile("ui/bag_base.png");
	autorelease();
	BagInit();
}

Bag::~Bag(){}

void Bag::BagInit()
{
	BagSlots.clear();
	int k = BAG_SLOT_TAG_BEGIN;
	for (int i = 0; i < 6; i++)
	{
		for (int j = 0; j != 15; j++)
		{
			Slot* BagSlot = new Slot();
			float plus = (BagSlot->getBoundingBox().size.width + 5) * j;
			float heightplus = i * (BagSlot->getBoundingBox().size.height + 5);
			BagSlot->setPosition(getContentSize().width - 28 - plus, getContentSize().height - 50 - heightplus);
			BagSlots[k] = BagSlot;
			BagSlot->setTag(k);
			addChild(BagSlot);
			k++;
		}
	}
	InitNumbers();
}

void Bag::LoadBagFromDB()
{
	std::string msg = "SELECT guid,Owner,Entry FROM inventory_instance WHERE Owner = ";
	char msga[50];
	snprintf(msga, 50, "%d AND slot >= 101 AND slot <= 190", m_owner->GetGuid());
	msg += msga;
	Result::Qurey result = Database::GetInstance()->GetResult(CharacterDatabase, msg.c_str());
	if (!result.empty())
	{
		for (Result::Qurey::iterator itr = result.begin(); itr != result.end(); itr++)
		{
			Item* TempItem = new Item(atoi(itr->second.at(2).c_str()), m_owner, atoi(itr->second.at(0).c_str()));
		}
	}
}

bool Bag::HasEmptySlot()
{
	bool HasEmpty = false;
	for (std::map<int, Slot*>::iterator itr = BagSlots.begin(); itr != BagSlots.end(); itr++)
	{
		if (itr->second->IsEmptySlot())
		{
			HasEmpty = true;
			break;
		}
	}
	return HasEmpty;
}

void Bag::SwapItem(Slot* OldSlot, Slot* NewSlot)
{
	Item* ChangeedItem = NewSlot->GetItem();
	Item* OldItem = OldSlot->GetItem();
	NewSlot->ReSetSlotItem();
	OldSlot->ReSetSlotItem();
	OldItem->setParent(nullptr);
	ChangeedItem->setParent(nullptr);
	OldItem->setPosition(NewSlot->getContentSize().width / 2, NewSlot->getContentSize().height / 2);
	ChangeedItem->setPosition(OldSlot->getContentSize().width / 2, OldSlot->getContentSize().height / 2);
	NewSlot->SetItem(OldItem);
	NewSlot->SetItemGuid(OldItem->GetGuid());
	OldSlot->SetItem(ChangeedItem);
	OldSlot->SetItemGuid(ChangeedItem->GetGuid());
	OldItem->SetSlot(NewSlot->getTag());
	ChangeedItem->SetSlot(OldSlot->getTag());
	NewSlot->ClearAllChildrenWithoutDelete();
	OldSlot->ClearAllChildrenWithoutDelete();
	OldSlot->addChild(ChangeedItem);
	NewSlot->addChild(OldItem);
}

Slot* Bag::GetSlot(int SlotNumber)
{
	if (BagSlots.find(SlotNumber) != BagSlots.end())
	{
		return BagSlots[SlotNumber];
	}
	return nullptr;
}

int Bag::GetEmptySlot()
{
	for (std::map<int, Slot*>::iterator itr = BagSlots.begin(); itr != BagSlots.end(); itr++)
	{
		if (itr->second->IsEmptySlot())
		{
			return itr->first;
		}
	}
	return -1;
}


void Bag::InitNumbers()
{
	int MoneyCount = Map_Scene::GetInstance()->GetLoadedPlayer()->GetInt32Value(MONEY);
	int JFCount = Map_Scene::GetInstance()->GetLoadedPlayer()->GetInt32Value(JF);

	char money[255], jf[255];
	snprintf(money, 255, "%d", MoneyCount);
	snprintf(jf, 255, "%d", JFCount);

	MoneyText = CCLabelTTF::create(money, "Airal", 11);
	JFText = CCLabelTTF::create(jf, "Airal", 11);

	MoneyText->setPosition(getPositionX() + getBoundingBox().size.width * 0.195f, getPositionY() + getBoundingBox().size.height * 0.05f);
	JFText->setPosition(getPositionX() + getBoundingBox().size.width * 0.6972f, getPositionY() + getBoundingBox().size.height * 0.05f);

	float sizeweibag = getBoundingBox().size.width;
	float y = getBoundingBox().size.height;

	MoneyText->setColor(ccc3(0, 0, 0));
	JFText->setColor(ccc3(0, 0, 0));

	MoneyText->setHorizontalAlignment(kCCTextAlignmentRight);
	JFText->setHorizontalAlignment(kCCTextAlignmentRight);

	addChild(MoneyText);
	addChild(JFText);
}

int Bag::GetTouchedSlot(Vec2 Ptouch)
{
	Vec2 touchLocation = Ptouch; // Get the touch position
	for (std::map<int, Slot*>::iterator itr = BagSlots.begin(); itr != BagSlots.end(); itr++)
	{
		if (itr == BagSlots.end())
			break;
		if (Slot* pSlot = itr->second)
		{
			Vec2 check = pSlot->getParent()->convertToNodeSpace(touchLocation);
			Rect bBox = pSlot->getBoundingBox();
			if (bBox.containsPoint(check))
				return itr->first;
		}
	}
	return BAG_TAG;
}


Slot::Slot(int ItemGuid, bool _isEquipSlot, int EquipSlotID)
{
	if (_isEquipSlot && EquipSlotID != -1)
	{
		switch (EquipSlotID)
		{
		case RING_ZERO:
		case RING_ONE:
		case RING_TWO:
		case RING_THR:
		case RING_FOR:
		case RING_FIVE:
			initWithFile("ui/PlayerEquip/Character_Equip_Finger.png");
			break;
		case NECKLACE_ZERO:
		case NECKLACE_ONE:
			initWithFile("ui/PlayerEquip/Character_Equip_NeckLace.png");
			break;
		case MAIN_HAND:
			initWithFile("ui/PlayerEquip/Character_Equip_Main_Weapon.png");
			break;
		case BELT:
			initWithFile("ui/PlayerEquip/Character_Equip_Belt.png");
			break;
		case HEAD:
			initWithFile("ui/PlayerEquip/Character_Equip_Head.png");
			break;
		case FACE:
			initWithFile("ui/PlayerEquip/Character_Equip_Face.png");
			break;
		case EYE:
			initWithFile("ui/PlayerEquip/Character_Equip_Eye.png");
			break;
		case JACKET:
			initWithFile("ui/PlayerEquip/Character_Equip_Jacket.png");
			break;
		case PANTS:
			initWithFile("ui/PlayerEquip/Character_Equip_Pants.png");
			break;
		case SHOES:
			initWithFile("ui/PlayerEquip/Character_Equip_Shoes.png");
			break;
		case PACKET:
			initWithFile("ui/PlayerEquip/Character_Equip_Pants.png");
			break;
		case EAR:
			initWithFile("ui/PlayerEquip/Character_Equip_Ear.png");
			break;
		case SHOULDER:
			initWithFile("ui/PlayerEquip/Character_Equip_Shoulder.png");
			break;
		case WRIST:
			initWithFile("ui/PlayerEquip/Character_Equip_Hand.png");
			break;
		case ROBOT:
			initWithFile("ui/PlayerEquip/Character_Equip_Robot.png");
			break;
		case ROBOT_HEART:
			initWithFile("ui/PlayerEquip/Character_Equip_Robot_Heart.png");
			break;
		case SOURCE:
			initWithFile("ui/PlayerEquip/Character_Equip_Source.png");
			break;
		case DADGE_ZERO:
			initWithFile("ui/PlayerEquip/Character_Equip_Badage_1.png");
			break;
		case DADGE_ONE:
			initWithFile("ui/PlayerEquip/Character_Equip_Badage_2.png");
			break;
		case OFF_HAND:
			initWithFile("ui/PlayerEquip/Character_Equip_Off_Hand.png");
			break;
		case BACK:
			initWithFile("ui/PlayerEquip/Character_Equip_Back.png");
			break;
		}
	}
	else
	{
		initWithFile("ui/bag_slot.png");
	}
	autorelease();
	m_Item = nullptr;
	m_ItemGuid = ItemGuid;
}

Slot::~Slot(){}

ComeFrom Slot::GetComeFrom()
{
	if (!m_Item)
		return COMEFROM_NULL;

	int Tag = m_Item->getTag();
	if (Tag > BAG_TAG && Tag <= BAG_SLOT_TAG_ENDED)
	{
		return COMEFROM_BAG;
	}
	if (Tag >= PLAYER_EQUIP_SLOT_START && Tag <= PLAYER_EQUIP_SLOT_END)
	{
		return COMEFROM_EQUIP;
	}

	return COMEFROM_NULL;
}